package top.kaoshanji.p2ch07a;

import top.kaoshanji.p2ch07a.support.Book;
import top.kaoshanji.p2ch07a.support.User;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 代码清单 p2ch07a-04：HashMap类造成对象存活时间过长的示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/19 20:07
 */
public class BookKeeper {

    private Map<Book, Set<User>> books = new HashMap<>();

    public BookKeeper(Book book, User user) {
        Set<User> users = null;
        if (books.containsKey(book)) {
            users = books.get(book);
        } else {
            users = new HashSet<>();
            books.put(book, users);
        }
        users.add(user);
    }

    public void returnBook(Book book, User user) {
        if (books.containsKey(book)) {
            Set<User> users = books.get(book);
            users.remove(user);
        }
    }

}
